---
import { DateTime } from 'luxon';
import { DashboardPage } from '../components/Dashboard/DashboardPage';
import BaseLayout from '../layouts/BaseLayout.astro';
import { getAllBestPractices } from '../lib/best-practice';
import { getAllQuestionGroups } from '../lib/question-group';
import { getAllVideos } from '../lib/video';
import { listOfficialGuides } from '../queries/official-guide';
import {
  isNewRoadmap,
  listOfficialRoadmaps,
} from '../queries/official-roadmap';
import type { BuiltInRoadmap } from '../components/Dashboard/PersonalDashboard';

const roadmaps = await listOfficialRoadmaps();
const roleRoadmaps = roadmaps.filter((roadmap) => roadmap.type === 'role');
const skillRoadmaps = roadmaps.filter((roadmap) => roadmap.type === 'skill');
const bestPractices = await getAllBestPractices();
const questionGroups = await getAllQuestionGroups();
const guides = await listOfficialGuides();
const videos = await getAllVideos();

const enrichedRoleRoadmaps: BuiltInRoadmap[] = roleRoadmaps.map((roadmap) => {
  return {
    id: roadmap.slug,
    url: `/${roadmap.slug}`,
    title: roadmap.title.card,
    description: roadmap.description,
    relatedRoadmapIds: roadmap.relatedRoadmaps,
    renderer: 'editor',
    isNew: isNewRoadmap(roadmap.createdAt),
    metadata: {
      tags: ['role-roadmap'],
    },
  };
});
const enrichedSkillRoadmaps: BuiltInRoadmap[] = skillRoadmaps.map((roadmap) => {
  return {
    id: roadmap.slug,
    url: `/${roadmap.slug}`,
    title: roadmap.title.card === 'Go' ? 'Go Roadmap' : roadmap.title.card,
    description: roadmap.description,
    relatedRoadmapIds: roadmap.relatedRoadmaps,
    renderer: 'editor',
    isNew: isNewRoadmap(roadmap.createdAt),
    metadata: {
      tags: ['skill-roadmap'],
    },
  };
});

const enrichedBestPractices = bestPractices.map((bestPractice) => {
  const { frontmatter } = bestPractice;

  return {
    id: bestPractice.id,
    url: `/best-practices/${bestPractice.id}`,
    title: frontmatter.briefTitle,
    description: frontmatter.briefDescription,
  };
});
---

<BaseLayout title='Dashboard' noIndex={true} permalink='/dashboard'>
  <DashboardPage
    builtInRoleRoadmaps={enrichedRoleRoadmaps}
    builtInSkillRoadmaps={enrichedSkillRoadmaps}
    builtInBestPractices={enrichedBestPractices}
    questionGroups={questionGroups}
    guides={guides.slice(0, 10)}
    videos={videos.slice(0, 10)}
    client:load
  />
  <div slot='open-source-banner'></div>
  <div slot='changelog-banner'></div>
</BaseLayout>
